import { Component, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { SettingsService, _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Api } from 'src/app/shared/api/api';
import { ResponseCode } from 'src/app/shared/response.code';
import { PublicService } from 'src/app/shared/service/public.service';

@Component({
  selector: 'aapp-system-resource-group',
  templateUrl: './resource-group.component.html'
})
export class ResourceGroupComponent implements OnInit {
  record: any = {};
  schema: SFSchema = {};
  parentId: string = '0';

  constructor(
    private modal: NzModalRef,
    public settings: SettingsService,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    private pubService: PublicService
  ) {}

  ngOnInit(): void {
    this.initSchema();
    console.log(this.parentId)
  }

  initSchema() {
    this.schema = {
      properties: {
        name: { type: 'string', title: '组名称', maxLength: 25, ui: { spanLabel: 5, spanControl: 19, grid: { span: 24 } } },
        remark: { type: 'string', title: '备注', maxLength: 25 }
      },
      required: ['name']
    };
  }

  save(value: any) {
    if (this.record.id) {
      value.id = this.record.id;
      this.http.put(`${Api.BaseResourceApi}/group`, value).subscribe((res: any) => {
        if (res) {
          if (res.code === ResponseCode.SUCCESS) {
            this.msgSrv.success('修改成功');
            this.modal.close(true);
          } else {
            this.msgSrv.warning(res.message);
          }
        } else {
          this.msgSrv.error('修改失败，未知错误');
        }
      });
    } else {
      value.parentId = this.parentId;
      this.http.post(`${Api.BaseResourceApi}/group`, value).subscribe((res: any) => {
        if (res) {
          if (res.code === ResponseCode.SUCCESS) {
            this.msgSrv.success('保存成功');
            this.modal.close(true);
          } else {
            this.msgSrv.warning(res.message);
          }
        } else {
          this.msgSrv.error('保存失败，未知错误');
        }
      });
    }
  }

  close() {
    this.modal.destroy();
  }
}
